﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_FireBolt : TargetSpell
    {
        //Fire// (Level 1)
        private int FireBaseDamageLowRank1 = 150;
        private int FireBaseDamageHighRank1 = 170;

        //(Level 7)
        private int FireBaseDamageLowRank2 = 230;
        private int FireBaseDamageHighRank2 = 270;

        //(Level 21) Range 100  Avg 450
        private int FireBaseDamageLowRank3 = 400;
        private int FireBaseDamageHighRank3 = 500;
        //Fire//


        //Ice// (Level 1)
        private int IceBaseDamageLowRank1 = 120;
        private int IceBaseDamageHighRank1 = 135;

        // (Level 7)
        private int IceBaseDamageLowRank2 = 170;
        private int IceBaseDamageHighRank2 = 190;
        //Ice//


        //Mentality//
        private int MentalityBaseDamageLowRank1 = 100;
        private int MentalityBaseDamageHighRank1 = 160;

        private int MentalityBaseDamageLowRank2 = 150;
        private int MentalityBaseDamageHighRank2 = 230;
        //Mentality//


        //Holy//

        //5
        private int HolyBaseDamageLowRank1 = 90;
        private int HolyBaseDamageHighRank1 = 110;

        //15
        private int HolyBaseDamageLowRank2 = 140;
        private int HolyBaseDamageHighRank2 = 165;
        //Holy//


        //Unholy// (Level 7)
        private int UnholyBaseDamageLowRank1 = 100;
        private int UnholyBaseDamageHighRank1 = 130;

        // (Level 25)  Range 50  Avg 400
        private int UnholyBaseDamageLowRank2 = 375;
        private int UnholyBaseDamageHighRank2 = 425;

        public int getBaseDamageHigh(SpellIcon _spellIcon)
        {
            switch (_spellIcon.Skill)
            {
                case "Fire":
                    if (_spellIcon.Name.IndexOf("Rank 2") > -1)
                        return FireBaseDamageHighRank2;
                    else if (_spellIcon.Name.IndexOf("Rank 3") > -1)
                        return FireBaseDamageHighRank3;
                    else
                        return FireBaseDamageHighRank1;

                case "Holy":
                    if (_spellIcon.Name.IndexOf("Rank 2") > -1)
                        return HolyBaseDamageHighRank2;
                    else
                        return HolyBaseDamageHighRank1;

                case "Ice":
                    if (_spellIcon.Name.IndexOf("Rank 2") > -1)
                        return IceBaseDamageHighRank2;
                    else
                        return IceBaseDamageHighRank1;

                case "Mentality":
                    if (_spellIcon.Name.IndexOf("Rank 2") > -1)
                        return MentalityBaseDamageHighRank2;
                    else
                        return MentalityBaseDamageHighRank1;

                case "Unholy":
                    if (_spellIcon.Name.IndexOf("Rank 2") > -1)
                        return UnholyBaseDamageHighRank2;
                    else
                        return UnholyBaseDamageHighRank1;

                default :
                    return 0;
            }
        }

        public int getBaseDamageLow(SpellIcon _spellIcon)
        {
            switch (_spellIcon.Skill)
            {
                case "Fire":
                    if (_spellIcon.Name.IndexOf("Rank 2") > -1)
                        return FireBaseDamageLowRank2;
                    else if (_spellIcon.Name.IndexOf("Rank 3") > -1)
                        return FireBaseDamageLowRank3;
                    else
                        return FireBaseDamageLowRank1;

                case "Holy":
                    if (_spellIcon.Name.IndexOf("Rank 2") > -1)
                        return HolyBaseDamageLowRank2;
                    else
                        return HolyBaseDamageLowRank1;

                case "Ice":
                    if (_spellIcon.Name.IndexOf("Rank 2") > -1)
                        return IceBaseDamageLowRank2;
                    else
                        return IceBaseDamageLowRank1;

                case "Mentality":
                    if (_spellIcon.Name.IndexOf("Rank 2") > -1)
                        return MentalityBaseDamageLowRank2;
                    else
                        return MentalityBaseDamageLowRank1;

                case "Unholy":
                    if (_spellIcon.Name.IndexOf("Rank 2") > -1)
                        return UnholyBaseDamageLowRank2;
                    else
                        return UnholyBaseDamageLowRank1;

                default:
                    return 0;
            }
        }

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, false, out MapItemsWithin12) == false)
                return;

            List<Character> CharactersInUpdateRange = new List<Character>();
            foreach (MapItem mapitem in MapItemsWithin12)
            {
                foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                {
                    if (creaturebase is Character)
                        CharactersInUpdateRange.Add((Character)creaturebase);
                }
            }

            int DamageAmount = myCreatureBase.GameHandler.random.Next(getBaseDamageLow(this.spellIcon), getBaseDamageHigh(this.spellIcon)  + 1);
            DamageAmount = SkillManager.AdjustForCombat(CastLevel, DamageAmount, myCreatureBase.IntelligenceTotal);
            DamageAmount = TargetCreatureBase.CalculateGetSpellDamagePerResists(DamageAmount, spellIcon.Skill);

            int absorbedDamage;
            TargetCreatureBase.InflictSpellDamage(DamageAmount,
                myCreatureBase is Character ? myCreatureBase.ID : -1,
                myCreatureBase is Creature ? myCreatureBase.CreatureID : -1,
                spellIcon.Skill, out absorbedDamage);
            
            TargetCreatureBase.Update_Character();

            DamageAmount = DamageAmount - absorbedDamage;

            if (myCreatureBase is Character)
            {
                Character myCharacter = (Character)myCreatureBase;

                Message healMsg1 = new Message();
                healMsg1.CreatureID = myCharacter.CreatureID;
                healMsg1.Type = "damage";
                if (spellIcon.Skill == "Fire")
                    healMsg1.Text = "You burn " + TargetCreatureBase.Name + "! (" + DamageAmount.ToString() + ")";
                else if (spellIcon.Skill == "Ice")
                    healMsg1.Text = "You freeze " + TargetCreatureBase.Name + "! (" + DamageAmount.ToString() + ")";
                else if (spellIcon.Skill == "Unholy")
                    healMsg1.Text = "You drain " + TargetCreatureBase.Name + "! (" + DamageAmount.ToString() + ")";
                else if (spellIcon.Skill == "Mentality")
                    healMsg1.Text = "You shock " + TargetCreatureBase.Name + "! (" + DamageAmount.ToString() + ")";
                else if (spellIcon.Skill == "Holy")
                    healMsg1.Text = "You smite " + TargetCreatureBase.Name + "! (" + DamageAmount.ToString() + ")";
                else if (spellIcon.Skill == "Unholy")
                    healMsg1.Text = "You sap " + TargetCreatureBase.Name + "! (" + DamageAmount.ToString() + ")";

                if (absorbedDamage != 0)
                    healMsg1.Text = healMsg1.Text + " ABSORBED " + absorbedDamage.ToString();

                myCharacter.AddMessage(healMsg1);
            }
            if (TargetCreatureBase is Character && TargetCreatureBase != myCreatureBase)
            {
                Character targetCharacter = (Character)TargetCreatureBase;

                Message healMsg1 = new Message();
                healMsg1.CreatureID = targetCharacter.CreatureID;
                healMsg1.Type = "damaged";
                if (spellIcon.Skill == "Fire")
                    healMsg1.Text = myCreatureBase.Name + " burns you! (" + DamageAmount.ToString() + ")";
                else if (spellIcon.Skill == "Ice")
                    healMsg1.Text = myCreatureBase.Name + " freezes you! (" + DamageAmount.ToString() + ")";
                else if (spellIcon.Skill == "Unholy")
                    healMsg1.Text = myCreatureBase.Name + " drains you! (" + DamageAmount.ToString() + ")";
                else if (spellIcon.Skill == "Mentality")
                    healMsg1.Text = myCreatureBase.Name + " shocks you! (" + DamageAmount.ToString() + ")";
                else if (spellIcon.Skill == "Holy")
                    healMsg1.Text = myCreatureBase.Name + " smites you! (" + DamageAmount.ToString() + ")";
                else if (spellIcon.Skill == "Unholy")
                    healMsg1.Text = myCreatureBase.Name + " saps you! (" + DamageAmount.ToString() + ")";

                if (absorbedDamage != 0)
                    healMsg1.Text = healMsg1.Text + " ABSORBED " + absorbedDamage.ToString();

                targetCharacter.AddMessage(healMsg1);
            }

            List<Character> CharsToUpdate = new List<Character>();
            MapItem mapitemToUpdate = TargetCreatureBase.currentMapItem;
            foreach (MapItem mapitem in MapItemsWithin12)
            {
                if (Math.Abs(mapitem.MapX - myCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - myCreatureBase.MapY) <= 4)
                {
                    foreach (CreatureBase cb in mapitem.CreaturesBases)
                    {
                        if (cb is Character)
                        {
                            CharsToUpdate.Add((Character)cb);
                        }
                    }
                }
                if (Math.Abs(mapitem.MapX - myCreatureBase.MapX) <= 8 && Math.Abs(mapitem.MapY - myCreatureBase.MapY) <= 8)
                {
                    foreach (CreatureBase cb in mapitem.CreaturesBases)
                    {
                        if (cb is Character)
                        {
                            Character character = (Character)cb;
                            if (spellIcon.Skill == "Fire")
                                character.PlaySound("FireBolt", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                            else if (spellIcon.Skill == "Ice")
                                character.PlaySound("IceBolt", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                            else if (spellIcon.Skill == "Mentality")
                                character.PlaySound("MindShock", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                            else if (spellIcon.Skill == "Holy")
                                character.PlaySound("Smite", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                            else if (spellIcon.Skill == "Unholy")
                                character.PlaySound("SoulSap", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                        }
                    }
                }
                if (TargetCreatureBase.HP < 1 && mapitem.MapX == TargetCreatureBase.MapX && mapitem.MapY == TargetCreatureBase.MapY)
                {
                    if (!(TargetCreatureBase is Character))
                    {
                        mapitem.CreaturesBases.Remove(TargetCreatureBase);
                    }
                    mapitemToUpdate = mapitem;
                }
            }
            foreach (Character character in CharsToUpdate)
            {
                if (TargetCreatureBase.HP < 1)
                    character.AddMapItem(mapitemToUpdate);
                else
                    character.UpdateCreatureBaseHp(TargetCreatureBase);
            }
        }
    }
}
